import swal from 'sweetalert'
export default {
  install(Vue,options){
    //v-noClick="{fn:login,time:2000}"
    //fn是回调函数，time是禁止点击时间
    Vue.directive('noClick',{
      bind:function(el,binding){
        const { value } = binding
        el.addEventListener('click',function(){
          console.log(1000);

          if(el.getAttribute('no_click') == 0){
            swal('请勿频繁点击');
          }else{
            setTimeout(function(){
              el.setAttribute('no_click',1);
            },value.time || 1000);
            el.setAttribute('no_click',0);
            value.fn ? value.fn() : value();
          }
        })
      }
    })
    Vue.filter('formatTime', function(val, format = 0, slice = '/'){
      const date = new Date(val);
      const repair = num => num.length === 1 ? '0' + num : num;
      const aFn = [
        () => date.getFullYear() + slice,
        () => repair(date.getMonth() + 1) + slice,
        () => repair(date.getDate()) + ' ',
        () => repair(date.getHours()) + ':',
        () => repair(date.getMinutes()) + ':',
        () => repair(date.getSeconds()),
      ]
      function oFn() {
        let str = '';
        Array.isArray(format) ?
          aFn.slice(format[0], format[1]).forEach(fn => str += fn()) :
          aFn.slice(format).forEach(fn => str += fn())
        return str;
      }
      return oFn();
    })
  }
}


